package com.tzbank.wisdom.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.input.BOMInputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.client.RestTemplate;

import com.netflix.config.validation.ValidationException;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.dao.LoanShelfMapper;
import com.tzbank.wisdom.service.LoanShelfService;
import com.tzbank.wisdom.utils.Util;
import com.tzbank.wisdom.utils.XmlUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service(value = "loanShelfService")
public class LoanShelfImpl implements LoanShelfService {

	@Autowired
    RestTemplate restTemplate;
	@Autowired
	public TransactionTemplate trsTemplate;
	@Autowired
	private LoanShelfMapper loanShelfMapper;
	
	public static String xmlFilePath;//xml文件路径
	public String saveImgPath;//图片路径
	public String loanLines;//产品线编号
	@Override
	public Map<String, Object> getLoanXmlProduct(List<Map> list) throws IOException, ValidationException{
		System.out.println(2222);
		
		//解析文件，获取属性值，更新数据库
		int falNum = 0;
		int nullNum = 0;
		StringBuffer errFileNames = new StringBuffer("");//同步错误文件名
		StringBuffer nullFileNames = new StringBuffer("");//文件为空的文件名
		for(Map loanHistMap : list){
			boolean synFlag = true;
			String synFileName = (String) loanHistMap.get("SynFileName");
		//	FileInputStream fis = null;
			BufferedReader fis =null ;
			try{
				fis =new BufferedReader( new InputStreamReader( new  BOMInputStream ( new FileInputStream("C:\\Users\\tzbank\\Desktop\\prod产品文件1.xml") ) ) );
				 
			}catch(IOException e){
				log.error("get xml File is null Error! " + synFileName);
				nullFileNames.append(synFileName + ", ");
				nullNum++;
				continue;
			}
			String bankId = "04590501";
			try{
				//将文件转换为字符串
			/*	byte[] xmlByte = new byte[fis.lines()];
				fis.read(xmlByte);
				String xmlStr = new String(xmlByte);
				*/
				StringBuffer xmlStr = new StringBuffer();
				String line =null;
				while((line=fis.readLine())!=null) {
					xmlStr.append(line);
					}
				//调用xml工具类，进行转换，转换成map
				log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 文件解析开始 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
				
				//Map<String, Object> map=getDateOfXML("C:\\Users\\tzbank\\Desktop\\20171221_6产品文件.xml");
				
			//	Map<String, Object> map = XmlUtil.getDateOfXML("C:\\Users\\tzbank\\Desktop\\prod产品文件2.xml");
				Map<String, Object> map = XmlUtil.xml2mapWithAttr(xmlStr.toString(), false);
				log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 文件解析结束 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
				
				//文件解析为空，抛出异常
				if(map == null || map.isEmpty()){
					log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 文件解析map为空，结束同步工作。 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
					throw new ValidationException("validation.file.empty");// 不再同步
				}
				//根据xml文件标签，获取对应的属性值
				//单独产品列表
				boolean prdFlag = false; //单独产品是否为空标志,默认不为空
				Map prodMap = (Map) map.get("productList");
				//产品列表为空，抛出异常
				if(prodMap == null || prodMap.isEmpty()){
					log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 单独产品列表为空，结束同步工作。 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
					prdFlag = true;
				}else{
					//获取产品列表
					Object prodListObj = (Object)prodMap.get("product");
					if(prodListObj == null){
						log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 单独产品为空，结束同步工作。 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
						prdFlag = true;
					}else{
						if(prodListObj instanceof List){
							List<Map> prodList = (List)prodListObj;
							for(Map prdMap : prodList){
								//多个产品，循环一个一个插入数据库
								log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 多个单独产品，同步工作 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
								insertPrd(prdMap, synFileName, bankId);
							}
							log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 多个单独产品，同步工作 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
						}else{
							//单个产品
							log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 单个单独产品，同步工作 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
							Map prdMap = (Map)prodListObj;
							insertPrd(prdMap, synFileName, bankId);
							log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 单个单独产品，同步工作 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
						}
					}
				}
				//套餐产品 列表
				boolean prdSetFlag = false; //套餐产品是否为空标志,默认不为空
				Map prodSetMap = (Map) map.get("productSetList");
				if(prodSetMap == null || prodSetMap.isEmpty()){
					log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 套餐产品列表为空，结束同步工作。 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
					prdSetFlag = true;
				}else{
					//获取产品列表
					Object prodSetListObj = (Object)prodSetMap.get("productSet");
					if(prodSetListObj == null){
						log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 套餐产品为空，结束同步工作。 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
						prdSetFlag = true;
					}else{
						if(prodSetListObj instanceof List){
							List<Map> prodSetList = (List)prodSetListObj;
							for(Map prdSetMap : prodSetList){
								//多个套餐产品，循环一个一个插入数据库
								log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 多个套餐产品，同步工作 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
								insertSetPrd(prdSetMap, synFileName, bankId);
							}
							log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 多个套餐产品，同步工作 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
						}else{
							//单个套餐产品
							log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 单个套餐产品，同步工作 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
							Map prdSetMap = (Map)prodSetListObj;
							insertSetPrd(prdSetMap, synFileName, bankId);
							log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 单个套餐产品，同步工作 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
						}
					}
				}
				if(prdFlag && prdSetFlag){
					//产品为空，抛出异常
					throw new ValidationException("validation.productList.empty");// 不再同步
				}
			}catch (Exception e) {
				log.error("Parse xml File Error! " + synFileName+"="+e);
				updateStatus(synFileName,loanHistMap,"F");
				synFlag = false;
				falNum++;
				errFileNames.append(synFileName + ", ");
//				throw new RuntimeException(e);
			}finally{
				fis.close();
			}
			if(synFlag){
				log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 文件："+synFileName+"同步工作完成 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
				//当前文件产品更新完毕，修改历史记录表的状态
				log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 修改文件："+synFileName+"的更新状态 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
				updateStatus(synFileName,loanHistMap,"C");
				log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 修改文件："+synFileName+"的更新状态 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			}
		}
		if(falNum > 0 || nullNum > 0){
			if(falNum > 0 && nullNum > 0){
				throw new ValidationException("本次同步" + list.size() + "个文件，其中有" + falNum + "个文件同步失败，有"+ nullNum +
						"个文件不存在，请检查。失败的文件名为：" + errFileNames.toString() + "不存在的文件名为：" + nullFileNames.toString());
			}else if(falNum > 0 && nullNum == 0){
				//存在同步失败的文件
				throw new ValidationException("本次同步" + list.size() + "个文件，其中有" + falNum + "个文件同步失败，请检查。失败的文件名为：" + errFileNames.toString());
			}else{
				throw new ValidationException("本次同步" + list.size() + "个文件，其中有"+ nullNum + "个文件不存在，请检查。不存在的文件名为：" + nullFileNames.toString());
			}
		}
		 return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), "同步解析成功");
	
	}

	/**
	 * 贷款产品信息获取，以便于同步操作
	 * @param prdMap 单个product标签
	 * @param synFileName 文件名称
	 * @throws ValidationException 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  void insertPrd(Map prdMap, String synFileName, String bankId) throws Exception{
		//单个产品的信息
		Map loanInfoMap = new HashMap();//贷款产品基本信息
		List<Map> loanComList = new ArrayList();//贷款产品组件信息
		List<Map> loanDetailList = new ArrayList();//贷款产品详细信息
		
		//获取产品线编号，如果不是贷款产品线，就直接结束
		Map infoMap = (Map)prdMap.get("productInfo");
		String prdLine = (String) (Util.isNullOrEmpty(infoMap.get("productLine")) ? "" : ((Map)infoMap.get("productLine")).get("@id"));//产品线编号
		if(!Util.isNullOrEmpty(loanLines)){
			String[] loanLine = loanLines.split(",");
			List<String> loanLineList = Arrays.asList(loanLine);
			if(prdLine == null || !loanLineList.contains(prdLine)){//产品线编号
				log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 非贷款产品，结束当前，进行下个产品。 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
				return;
			}
		}
		
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品基本信息组装 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		//一、产品基本属性信息
		String loanPrdId = (String)prdMap.get("@id");//产品编号
		String loanPrdName = (String)prdMap.get("@name");//产品名称
		
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 当前贷款产品编号："+loanPrdId+",贷款产品名称："+loanPrdName+" >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		
		String loanPrdStatus = (String)prdMap.get("@status");//产品状态 0:维护完成 1:新增  2:待下架
		
		//判断产品状态是否为下架状态
		if("2".equals(loanPrdStatus)){
			//产品状态为待下架状态，则数据库执行状态更新
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品状态为待下架，修改数据库产品状态 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品数据库状态修改 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			final String prdId = loanPrdId;
			final String prdStatus = loanPrdStatus;
			final String fileName = synFileName;
			final String bkId = bankId;
			trsTemplate.execute(new TransactionCallback(){
				public Object doInTransaction(TransactionStatus arg0) {
					updateLoanShelfPrdState(prdId,prdStatus,fileName,bkId);
					return null;
				}
			});
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品数据库状态修改 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			return;
		}
		
		String prdTeam = (String)((Map)infoMap.get("productTeam")).get("@id");//产品组编号
		String prdBasePrd = (String)((Map)infoMap.get("baseProduct")).get("@id");//父级基础产品，基础产品编号
		String pubDate = (String)infoMap.get("publishDate");//发布时间
		String crmNum = (String) (Util.isNullOrEmpty(infoMap.get("crmNum")) ? "" : ((Map)infoMap.get("crmNum")).get("@val"));//CRM产品树编号
		String cateNum = (String) (Util.isNullOrEmpty(infoMap.get("cateNum")) ? "" : ((Map)infoMap.get("cateNum")).get("@val"));//业务品种编号
		String subCateNum = (String) (Util.isNullOrEmpty(infoMap.get("subCateNum")) ? "" : ((Map)infoMap.get("subCateNum")).get("@val"));//子业务品种编号
		loanInfoMap.put("LoanPrdId", loanPrdId);
		loanInfoMap.put("LoanPrdName", loanPrdName);
		loanInfoMap.put("LoanPrdStatus", loanPrdStatus);
		loanInfoMap.put("LoanPrdLine", prdLine);
		loanInfoMap.put("LoanPrdTeam", prdTeam);
		loanInfoMap.put("LoanPrdBasePrd", prdBasePrd);
		loanInfoMap.put("LoanPublishDate", pubDate);
		loanInfoMap.put("LoanCrmNum", crmNum);
		loanInfoMap.put("LoanCateNum", cateNum);
		loanInfoMap.put("LoanSubCateNum", subCateNum);
		
		//(1)首页展示热销图片
		//热销标志
		Map labelsMap = (Map)prdMap.get("labels");
		if(labelsMap != null && !labelsMap.isEmpty()){
			Object hotMapObj = (Object)(labelsMap.get("label"));
			if(!Util.isNullOrEmpty(hotMapObj)){
				if(hotMapObj instanceof List){
					List<Map> hotMaps = (List)hotMapObj;
					for(int i = 0; i < hotMaps.size(); i++){
						Map hotMap = hotMaps.get(i);
						String lableId = (String)hotMap.get("@id");//热销标志
						if(lableId != null && "LABEL00002".equals(lableId)){
							String hotDegree = (String)hotMap.get("@degree");//热销等级
							loanInfoMap.put("LoanHotSellFlag", "1");//1热销
							loanInfoMap.put("LoanHotSellDegree", hotDegree);
						}
					}
				}else{
					Map hotMap = (Map)hotMapObj;
					String lableId = (String)hotMap.get("@id");//热销标志
					if(lableId != null && "LABEL00002".equals(lableId)){
						String hotDegree = (String)hotMap.get("@degree");//热销等级
						loanInfoMap.put("LoanHotSellFlag", "1");//1热销
						loanInfoMap.put("LoanHotSellDegree", hotDegree);
					}
				}
			}
		}
		Object displayObj = (Object)prdMap.get("display");
		if(!Util.isNullOrEmpty(displayObj)){
			Map displayMap = (Map)displayObj;
			//热销图片
			StringBuffer hotImg = new StringBuffer("");//热销图片
			Map hotImgsMap = (Map)displayMap.get("homeDisplayPictures");
			if(hotImgsMap != null && !hotImgsMap.isEmpty()){
				Object hotImgsObj = (Object)((hotImgsMap).get("picture"));
				if(!Util.isNullOrEmpty(hotImgsObj)){
					if(hotImgsObj instanceof List){
						List<String> hotImgs = (List)hotImgsObj;
						for(int i = 0; i < hotImgs.size(); i++){
							String hostImg = hotImgs.get(i);
							if(i < hotImgs.size()-1){
								hotImg = hotImg.append(hostImg + ",");
							}else{
								hotImg = hotImg.append(hostImg);
							}
						}
					}else{
						String hostImg = (String)hotImgsObj;
						hotImg = new StringBuffer(hostImg);
					}
				}
				loanInfoMap.put("LoanHotSellImg", hotImg.toString());
			}
			
			//(2)列表展示产品列表
			Object listPreObj = (Object)displayMap.get("listDisplay");
			if(!Util.isNullOrEmpty(listPreObj)){
				Map listPreMap = (Map)listPreObj;
				//(a)列表图片
				StringBuffer listImg = new StringBuffer("");//列表图片
				//取标识，确定为图片文件 0图片 1视频 2说明书、协议
				Map listImgsMap = (Map)listPreMap.get("listImages");
				if(listImgsMap != null && !listImgsMap.isEmpty()){
					Object listImgsObj = (Object)((listImgsMap).get("picture"));
					if(!Util.isNullOrEmpty(listImgsObj)){
						if(listImgsObj instanceof List){
							List<String> listImgs = (List)listImgsObj;
							for(int i = 0; i < listImgs.size(); i++){
								String lisImg = listImgs.get(i);
								if(i < listImgs.size()-1){
									listImg = listImg.append(lisImg + ",");
								}else{
									listImg = listImg.append(lisImg);
								}
							}
						}else{
							String lisImg = (String)listImgsObj;
							listImg = new StringBuffer(lisImg);
						}
					}
				}
				loanInfoMap.put("LoanListImg", listImg.toString());
				
				//(b)列表文字
				Map prdPhraseMap = (Map)listPreMap.get("productPhrase");//列表短语
				StringBuffer prdPhrase = new StringBuffer("");
				if(prdPhraseMap != null && !prdPhraseMap.isEmpty()){
					Object phraseObj = (Object)(prdPhraseMap.get("phrase"));
					if(!Util.isNullOrEmpty(phraseObj)){
						if(phraseObj instanceof List){
							List<String> phraseList = (List)phraseObj;
							for(int i = 0; i < phraseList.size(); i++){
								if(i < phraseList.size()-1){
									prdPhrase = prdPhrase.append(phraseList.get(i)).append(",");
								}else{
									prdPhrase = prdPhrase.append(phraseList.get(i));
								}
							}
						}else{
							String phrase = (String)phraseObj;
							prdPhrase = prdPhrase.append(phrase);
						}
					}
				}
				loanInfoMap.put("LoanPrdPhrase", prdPhrase.toString());
				
				String limitDesc = (String)listPreMap.get("limitDesc");//描述1
				String termDesc = (String)listPreMap.get("termDesc");//描述2
				StringBuffer prdDesc = new StringBuffer("");
				if(limitDesc != null && !"".equals(limitDesc)){
					prdDesc = prdDesc.append(limitDesc).append(" | ");
				}
				if(termDesc != null && !"".equals(termDesc)){
					prdDesc = prdDesc.append(termDesc).append(" | ");
				}
				Map listFeatureMap = (Map)listPreMap.get("listFeature");
				if(listFeatureMap != null && !listFeatureMap.isEmpty()){
					Object featureObj = (Object)(listFeatureMap.get("feature"));
					if(!Util.isNullOrEmpty(featureObj)){
						if(featureObj instanceof List){
							List<String> featurList = (List)featureObj;
							for(String str : featurList){
								prdDesc = prdDesc.append(str).append(" | ");
							}
						}else{
							String feature = (String)featureObj;
							prdDesc = prdDesc.append(feature).append(" | ");
						}
					}
				}
				String loanPrdDesc = prdDesc.toString();//综合描述语
				if(loanPrdDesc != null && !"".equals(loanPrdDesc)){
					loanPrdDesc = loanPrdDesc.substring(0, loanPrdDesc.length()-3);
				}
				loanInfoMap.put("LoanPrdDesc", loanPrdDesc);
			}
			
			//(3)详情页展示信息
			//(a)详情页图片
			Map detailImgsMap = (Map)displayMap.get("detailsDisplay");
			if(detailImgsMap != null && !detailImgsMap.isEmpty()){
				StringBuffer detailImg = new StringBuffer("");//详情页图片
				Object detailImgsObj = (Object)((detailImgsMap).get("picture"));
				if(!Util.isNullOrEmpty(detailImgsObj)){
					if(detailImgsObj instanceof List){
						List<String> detailImgs = (List)detailImgsObj;
						for(int i = 0; i < detailImgs.size(); i++){
							String detiImg = detailImgs.get(i);
							if(i < detailImgs.size()-1){
								detailImg = detailImg.append(detiImg + ",");
							}else{
								detailImg = detailImg.append(detiImg);
							}
						}
					}else{
						String detiImg = (String)detailImgsObj;
						detailImg = new StringBuffer(detiImg);
					}
				}
				loanInfoMap.put("LoanDetailImg", detailImg.toString());
			}
			
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品详情页标题内容组装 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			//三、详情页标题内容
			Map detailInfoMap = (Map)displayMap.get("productDetail");
			if(detailInfoMap == null || detailInfoMap.isEmpty()){
				log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 产品详细信息为空，结束同步工作。 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
//				throw new ValidationException("validation.productDetail.empty");// 不再同步
			}else{
				Object detailInfoListObj = (Object)((detailInfoMap).get("detail"));
				if(!Util.isNullOrEmpty(detailInfoListObj)){
					if(detailInfoListObj instanceof List){
						List<Map> detailInfoList = (List)detailInfoListObj;
						for(int i= 0; i < detailInfoList.size(); i++){
							Map detailMap = detailInfoList.get(i);
							Map mm = getDetailInfoMap(detailMap,loanPrdId);
							if(mm != null && !mm.isEmpty()){
								String titleType = (String)mm.get("TitleType");
								if(!"0".equals(titleType)){
									String detailContext = (String)mm.get("Context");
									mm.put("Context", detailContext);
									mm.put("ContextFileName", detailContext.substring(detailContext.lastIndexOf("_")+1));
								}
								loanDetailList.add(mm);
							}
						}
					}else{
						Map detailMap = (Map)detailInfoListObj;
						Map mm = getDetailInfoMap(detailMap,loanPrdId);
						if(mm != null && !mm.isEmpty()){
							String titleType = (String)mm.get("TitleType");
							if(!"0".equals(titleType)){
								String detailContext = (String)mm.get("Context");
								mm.put("Context", detailContext);
								mm.put("ContextFileName", detailContext.substring(detailContext.lastIndexOf("_")+1));
							}
							loanDetailList.add(mm);
						}
					}
				}
			}
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品详情页标题内容组装 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			if(loanDetailList == null || loanDetailList.size() == 0){
				log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品详情页标题内容为空，抛出异常，同步失败 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
//				throw new ValidationException("validation.loanDetail.empty");// 不再同步
			}
			loanInfoMap.put("IsShelfFlag", "1");//货架展示标识 1:展示货架 0：不展示货架
		}else{
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品货架内容为空，即xml文件未配置display的内容 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			loanInfoMap.put("IsShelfFlag", "0"); //不展示货架
		}
		
		//(4)产品功能，描述
		Object prdFuncObj = (Object)prdMap.get("productFuncs");
		if(!Util.isNullOrEmpty(prdFuncObj) && (prdFuncObj instanceof Map)){
			Map prdFuncMap = (Map)prdFuncObj;
			if(prdFuncMap != null && !prdFuncMap.isEmpty()){
				StringBuffer prdFuncName = new StringBuffer("");//产品功能名称(多个，用','隔开)
				StringBuffer prdFuncId = new StringBuffer("");//产品功能Id(多个，用','隔开)
				Object prdFuncListObj = (Object)(prdFuncMap).get("func");
				if(!Util.isNullOrEmpty(prdFuncListObj)){
					if(prdFuncListObj instanceof List){
						List<Map> prdFuncList = (List)prdFuncListObj;
						if(prdFuncList != null && prdFuncList.size() > 0){
							for(int i = 0; i < prdFuncList.size(); i++){
								Map funcMap = prdFuncList.get(i);
								String funcName = (String)funcMap.get("@name");
								String funcId = (String)funcMap.get("@funcId");
								if(i < prdFuncList.size()-1){
									prdFuncName = prdFuncName.append(funcName+",");
									prdFuncId = prdFuncId.append(funcId+",");
								}else{
									prdFuncName = prdFuncName.append(funcName);
									prdFuncId = prdFuncId.append(funcId);
								}
							}
						}
					}else{
						Map funcMap = (Map)prdFuncListObj;
						String funcName = (String)funcMap.get("@name");
						String funcId = (String)funcMap.get("@funcId");
						prdFuncName = prdFuncName.append(funcName);
						prdFuncId = prdFuncId.append(funcId);
					}
				}
				loanInfoMap.put("LoanFuncName", prdFuncName.toString());
				loanInfoMap.put("LoanFuncId", prdFuncId.toString());
			}
		}
		loanInfoMap.put("XMLFileName", synFileName);
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品基本信息组装 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		if(loanInfoMap == null || loanInfoMap.isEmpty()){
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品基本信息为空，抛出异常，同步失败 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			throw new ValidationException("validation.loanInfo.empty");// 不再同步
		}
		
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品组件信息组装 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		//二、组件信息
		Object prdComObj = (Object)prdMap.get("productCom");
		if(prdComObj == null){
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 产品组件为空，结束同步工作。 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
//			throw new ValidationException("validation.productCom.empty");// 不再同步
		}else{
			if(prdComObj instanceof List){
				List<Map> prdComList = (List)prdComObj;
				for(Map comMap : prdComList){
					List<Map> comMapList = getPrdComList(comMap,loanPrdId);
					if(comMapList != null && comMapList.size()>0){
						loanComList.addAll(comMapList);
					}
				}
			}else{
				Map comMap = (Map)prdComObj;
				List<Map> comMapList = getPrdComList(comMap,loanPrdId);
				if(comMapList != null && comMapList.size()>0){
					loanComList.addAll(comMapList);
				}
			}
		}
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品组件信息组装 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		if(loanComList == null || loanComList.size() == 0){
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品组件信息为空，抛出异常，同步失败 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
//			throw new ValidationException("validation.loanCom.empty");// 不再同步
		}
		
		
		//单个贷款产品信息获取完毕，进行数据库插入操作
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品数据库同步 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		insertLoanShelfTable(loanPrdId,loanInfoMap,loanComList,loanDetailList,bankId);
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品数据库同步 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		
		//插入数据库完毕，该产品的所有图片或文件由渠道端自己下载
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款插入数据库完毕，该产品的所有图片或文件由渠道端自己下载begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
	}
	
	
	/**
	 * 套餐产品信息获取，以便于同步操作
	 * @param prdSetMap 单个productSet标签
	 * @param synFileName 文件名称
	 * @throws ValidationException
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public   void insertSetPrd(Map prdSetMap, String synFileName, String bankId) throws ValidationException{
		//单个套餐产品的信息
		Map loanSetInfoMap = new HashMap();//套餐产品基本信息
		List<Map> loanSetDetailList = new ArrayList();//套餐产品详细信息
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品基本信息组装 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		//一、套餐产品基本属性信息
		String loanPrdId = (String)prdSetMap.get("@productSetId");//产品编号
		String loanPrdName = (String)prdSetMap.get("@productSetName");//产品名称
		
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 当前贷款产品编号："+loanPrdId+",贷款产品名称："+loanPrdName+" >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		
		String loanPrdStatus = Util.isNullOrEmpty(prdSetMap.get("@status")) ? "1" : (String)prdSetMap.get("@status");//产品状态 0:维护完成 1:新增  2:待下架
		
		//判断产品状态是否为下架状态
		if("2".equals(loanPrdStatus)){
			//产品状态为待下架状态，则数据库执行状态更新
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品状态为待下架，修改数据库产品状态 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品数据库状态修改 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			final String prdId = loanPrdId;
			final String prdStatus = loanPrdStatus;
			final String fileName = synFileName;
			final String bkId = bankId;
			trsTemplate.execute(new TransactionCallback(){
				public Object doInTransaction(TransactionStatus arg0) {
					updateLoanShelfPrdState(prdId,prdStatus,fileName,bkId);
					return null;
				}
			});
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品数据库状态修改 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			return;
		}
		
		loanSetInfoMap.put("LoanPrdId", loanPrdId);
		loanSetInfoMap.put("LoanPrdName", loanPrdName);
		loanSetInfoMap.put("LoanPrdStatus", loanPrdStatus);
		loanSetInfoMap.put("LoanPrdLine", "LOANSET");
		
		//(1)首页展示热销图片
		//热销标志
		Map labelsMap = (Map)prdSetMap.get("labels");
		if(labelsMap != null && !labelsMap.isEmpty()){
			Object hotMapObj = (Object)(labelsMap.get("label"));
			if(!Util.isNullOrEmpty(hotMapObj)){
				if(hotMapObj instanceof List){
					List<Map> hotMaps = (List)hotMapObj;
					for(int i = 0; i < hotMaps.size(); i++){
						Map hotMap = hotMaps.get(i);
						String lableId = (String)hotMap.get("@id");//热销标志
						if(lableId != null && "LABEL00002".equals(lableId)){
							String hotDegree = (String)hotMap.get("@degree");//热销等级
							loanSetInfoMap.put("LoanHotSellFlag", "1");//1热销
							loanSetInfoMap.put("LoanHotSellDegree", hotDegree);
						}
					}
				}else{
					Map hotMap = (Map)hotMapObj;
					String lableId = (String)hotMap.get("@id");//热销标志
					if(lableId != null && "LABEL00002".equals(lableId)){
						String hotDegree = (String)hotMap.get("@degree");//热销等级
						loanSetInfoMap.put("LoanHotSellFlag", "1");//1热销
						loanSetInfoMap.put("LoanHotSellDegree", hotDegree);
					}
				}
			}
		}
		Object displayObj = (Object)prdSetMap.get("display");
		if(!Util.isNullOrEmpty(displayObj)){
			Map displayMap = (Map)displayObj;
			//热销图片
			StringBuffer hotImg = new StringBuffer("");//热销图片
			Map hotImgsMap = (Map)displayMap.get("homeDisplayPictures");
			if(hotImgsMap != null && !hotImgsMap.isEmpty()){
				Object hotImgsObj = (Object)((hotImgsMap).get("picture"));
				if(!Util.isNullOrEmpty(hotImgsObj)){
					if(hotImgsObj instanceof List){
						List<String> hotImgs = (List)hotImgsObj;
						for(int i = 0; i < hotImgs.size(); i++){
							String hostImg = hotImgs.get(i);
							if(i < hotImgs.size()-1){
								hotImg = hotImg.append(hostImg + ",");
							}else{
								hotImg = hotImg.append(hostImg);
							}
						}
					}else{
						String hostImg = (String)hotImgsObj;
						hotImg = new StringBuffer(hostImg);
					}
				}
				loanSetInfoMap.put("LoanHotSellImg", hotImg.toString());
			}
			
			//(2)列表展示产品列表
			Object listPreObj = (Object)displayMap.get("listDisplay");
			if(!Util.isNullOrEmpty(listPreObj)){
				Map listPreMap = (Map)listPreObj;
				//(a)列表图片
				StringBuffer listImg = new StringBuffer("");//列表图片
				//取标识，确定为图片文件 0图片 1视频 2说明书、协议
				Map listImgsMap = (Map)listPreMap.get("listImages");
				if(listImgsMap != null && !listImgsMap.isEmpty()){
					Object listImgsObj = (Object)((listImgsMap).get("picture"));
					if(!Util.isNullOrEmpty(listImgsObj)){
						if(listImgsObj instanceof List){
							List<String> listImgs = (List)listImgsObj;
							for(int i = 0; i < listImgs.size(); i++){
								String lisImg = listImgs.get(i);
								if(i < listImgs.size()-1){
									listImg = listImg.append(lisImg + ",");
								}else{
									listImg = listImg.append(lisImg);
								}
							}
						}else{
							String lisImg = (String)listImgsObj;
							listImg = new StringBuffer(lisImg);
						}
					}
				}
				loanSetInfoMap.put("LoanListImg", listImg.toString());
				
				//(b)列表文字
				Map prdPhraseMap = (Map)listPreMap.get("productPhrase");//列表短语
				StringBuffer prdPhrase = new StringBuffer("");
				if(prdPhraseMap != null && !prdPhraseMap.isEmpty()){
					Object phraseObj = (Object)(prdPhraseMap.get("phrase"));
					if(!Util.isNullOrEmpty(phraseObj)){
						if(phraseObj instanceof List){
							List<String> phraseList = (List)phraseObj;
							for(int i = 0; i < phraseList.size(); i++){
								if(i < phraseList.size()-1){
									prdPhrase = prdPhrase.append(phraseList.get(i)).append(",");
								}else{
									prdPhrase = prdPhrase.append(phraseList.get(i));
								}
							}
						}else{
							String phrase = (String)phraseObj;
							prdPhrase = prdPhrase.append(phrase);
						}
					}
				}
				loanSetInfoMap.put("LoanPrdPhrase", prdPhrase.toString());
				
				String limitDesc = (String)listPreMap.get("limitDesc");//描述1
				String termDesc = (String)listPreMap.get("termDesc");//描述2
				StringBuffer prdDesc = new StringBuffer("");
				if(limitDesc != null && !"".equals(limitDesc)){
					prdDesc = prdDesc.append(limitDesc).append(" | ");
				}
				if(termDesc != null && !"".equals(termDesc)){
					prdDesc = prdDesc.append(termDesc).append(" | ");
				}
				Map listFeatureMap = (Map)listPreMap.get("listFeature");
				if(listFeatureMap != null && !listFeatureMap.isEmpty()){
					Object featureObj = (Object)(listFeatureMap.get("feature"));
					if(!Util.isNullOrEmpty(featureObj)){
						if(featureObj instanceof List){
							List<String> featurList = (List)featureObj;
							for(String str : featurList){
								prdDesc = prdDesc.append(str).append(" | ");
							}
						}else{
							String feature = (String)featureObj;
							prdDesc = prdDesc.append(feature).append(" | ");
						}
					}
				}
				String loanPrdDesc = prdDesc.toString();//综合描述语
				if(loanPrdDesc != null && !"".equals(loanPrdDesc)){
					loanPrdDesc = loanPrdDesc.substring(0, loanPrdDesc.length()-3);
				}
				loanSetInfoMap.put("LoanPrdDesc", loanPrdDesc);
			}
			
			//(3)详情页展示信息
			//(a)详情页图片
			Map detailImgsMap = (Map)displayMap.get("detailsDisplay");
			if(detailImgsMap != null && !detailImgsMap.isEmpty()){
				StringBuffer detailImg = new StringBuffer("");//详情页图片
				Object detailImgsObj = (Object)((detailImgsMap).get("picture"));
				if(!Util.isNullOrEmpty(detailImgsObj)){
					if(detailImgsObj instanceof List){
						List<String> detailImgs = (List)detailImgsObj;
						for(int i = 0; i < detailImgs.size(); i++){
							String detiImg = detailImgs.get(i);
							if(i < detailImgs.size()-1){
								detailImg = detailImg.append(detiImg + ",");
							}else{
								detailImg = detailImg.append(detiImg);
							}
						}
					}else{
						String detiImg = (String)detailImgsObj;
						detailImg = new StringBuffer(detiImg);
					}
				}
				loanSetInfoMap.put("LoanDetailImg", detailImg.toString());
			}
			
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品详情页标题内容组装 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			//三、详情页标题内容
			Map detailInfoMap = (Map)displayMap.get("productDetail");
			if(detailInfoMap == null || detailInfoMap.isEmpty()){
				log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 套餐产品详细信息为空，结束同步工作。 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			}else{
				Object detailInfoListObj = (Object)((detailInfoMap).get("detail"));
				if(!Util.isNullOrEmpty(detailInfoListObj)){
					if(detailInfoListObj instanceof List){
						List<Map> detailInfoList = (List)detailInfoListObj;
						for(int i= 0; i < detailInfoList.size(); i++){
							Map detailMap = detailInfoList.get(i);
							Map mm = getDetailInfoMap(detailMap,loanPrdId);
							if(mm != null && !mm.isEmpty()){
								String titleType = (String)mm.get("TitleType");
								if(!"0".equals(titleType)){
									String detailContext = (String)mm.get("Context");
									mm.put("Context", detailContext);
									mm.put("ContextFileName", detailContext.substring(detailContext.lastIndexOf("_")+1));
								}
								loanSetDetailList.add(mm);
							}
						}
					}else{
						Map detailMap = (Map)detailInfoListObj;
						Map mm = getDetailInfoMap(detailMap,loanPrdId);
						if(mm != null && !mm.isEmpty()){
							String titleType = (String)mm.get("TitleType");
							if(!"0".equals(titleType)){
								String detailContext = (String)mm.get("Context");
								mm.put("Context", detailContext);
								mm.put("ContextFileName", detailContext.substring(detailContext.lastIndexOf("_")+1));
							}
							loanSetDetailList.add(mm);
						}
					}
				}
			}
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品详情页标题内容组装 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			if(loanSetDetailList == null || loanSetDetailList.size() == 0){
				log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品详情页标题内容为空，抛出异常，同步失败 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			}
		}else{
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品货架内容为空，即xml文件未配置display的内容 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		}
		
		//(4)产品功能，描述
		Object prdFuncObj = (Object)prdSetMap.get("productFuncs");
		if(!Util.isNullOrEmpty(prdFuncObj) && (prdFuncObj instanceof Map)){
			Map prdFuncMap = (Map)prdFuncObj;
			if(prdFuncMap != null && !prdFuncMap.isEmpty()){
				StringBuffer prdFuncName = new StringBuffer("");//产品功能名称(多个，用','隔开)
				StringBuffer prdFuncId = new StringBuffer("");//产品功能Id(多个，用','隔开)
				Object prdFuncListObj = (Object)(prdFuncMap).get("func");
				if(!Util.isNullOrEmpty(prdFuncListObj)){
					if(prdFuncListObj instanceof List){
						List<Map> prdFuncList = (List)prdFuncListObj;
						if(prdFuncList != null && prdFuncList.size() > 0){
							for(int i = 0; i < prdFuncList.size(); i++){
								Map funcMap = prdFuncList.get(i);
								String funcName = (String)funcMap.get("@name");
								String funcId = (String)funcMap.get("@funcId");
								if(i < prdFuncList.size()-1){
									prdFuncName = prdFuncName.append(funcName+",");
									prdFuncId = prdFuncId.append(funcId+",");
								}else{
									prdFuncName = prdFuncName.append(funcName);
									prdFuncId = prdFuncId.append(funcId);
								}
							}
						}
					}else{
						Map funcMap = (Map)prdFuncListObj;
						String funcName = (String)funcMap.get("@name");
						String funcId = (String)funcMap.get("@funcId");
						prdFuncName = prdFuncName.append(funcName);
						prdFuncId = prdFuncId.append(funcId);
					}
				}
				loanSetInfoMap.put("LoanFuncName", prdFuncName.toString());
				loanSetInfoMap.put("LoanFuncId", prdFuncId.toString());
			}
		}
		loanSetInfoMap.put("XMLFileName", synFileName);
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品基本信息组装 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		if(loanSetInfoMap == null || loanSetInfoMap.isEmpty()){
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品基本信息为空，抛出异常，同步失败 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			throw new ValidationException("validation.loanInfo.empty");// 不再同步
		}
		
		//单个贷款套餐产品信息获取完毕，进行数据库插入操作
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品数据库同步 begin >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		insertLoanSetShelfTable(loanPrdId,loanSetInfoMap,loanSetDetailList,bankId);
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款套餐产品数据库同步 end >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
	}

	/**
	 * 判断是修改还是新增操作
	 * @param loanPrdId 产品编号
	 * @param loanInfoMap 产品基础信息
	 * @param loanComList 产品组件信息
	 * @param loanDetailList 产品详细信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
				 
	public  void insertLoanShelfTable(final String loanPrdId,final Map loanInfoMap,final List<Map> loanComList,final List<Map> loanDetailList, final String bankId){
		//获取产品编号，查询数据库是否存在，若存在，则说明做的是更新操作，不存在，则是新增操作
		Map<String,String> qryMap = new HashMap<String,String>();
		qryMap.put("LoanPrdCode", loanPrdId);
		qryMap.put("BankId", bankId);
		int prdCount =loanShelfMapper.selectLoanPrdInfoCount(qryMap);
		
		if(prdCount > 0 ){
			//存在数据，先删除，再新增
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 数据库存在编号为："+loanPrdId+" 的该贷款产品数据，先删除，再新增 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			trsTemplate.execute(new TransactionCallback(){
				public Object doInTransaction(TransactionStatus arg0) {
					deleteLoanShelfPrdById(loanPrdId, bankId);
					addLoanShelf(loanPrdId,loanInfoMap,loanComList,loanDetailList,bankId);
					return null;
				}
			});
		
		}else{
			
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 数据库不存在编号为："+loanPrdId+" 的该贷款产品数据，直接新增 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			trsTemplate.execute(new TransactionCallback(){
					public Object doInTransaction(TransactionStatus arg0) {
						addLoanShelf(loanPrdId,loanInfoMap,loanComList,loanDetailList,bankId);
						return null;
					}
				});
		}
	}
	
	/**
	 * 产品存入数据库
	 * @param loanPrdId 产品编号
	 * @param loanInfoMap 产品基础信息
	 * @param loanComList 产品组件信息
	 * @param loanDetailList 产品详细信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public   void addLoanShelf(String loanPrdId, Map loanInfoMap, List<Map> loanComList, List<Map> loanDetailList, String bankId){
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品基础信息插入 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		loanInfoMap.put("BankId", bankId);
		int a=loanShelfMapper.insertLoanShelf(loanInfoMap);
	//	this.ibsSqlMap.insert("batch.insertLoanShelf", loanInfoMap);
		for(int i = 0; i < loanComList.size(); i++){
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 当前为第"+(i+1)+"个贷款产品组件插入 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			Map comMap = loanComList.get(i);
			comMap.put("BankId", bankId);
			int com=loanShelfMapper.insertLoanShelfCom(comMap);
	//		this.ibsSqlMap.insert("batch.insertLoanShelfCom", comMap);
		}
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品详细信息插入 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		for(int i = 0; i < loanDetailList.size(); i++){
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 当前为第"+(i+1)+"个贷款产品详情插入 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			Map detailMap = loanDetailList.get(i);
			detailMap.put("BankId", bankId);
			loanShelfMapper.insertLoanShelfDetail(detailMap);
	//		this.ibsSqlMap.insert("batch.insertLoanShelfDetail", detailMap);
		}
	}
	
	
	/**
	 * 判断套餐产品是修改还是新增操作
	 * @param loanPrdId 套餐产品编号
	 * @param loanSetInfoMap 套餐产品基础信息
	 * @param loanSetDetailList 套餐产品详细信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  void insertLoanSetShelfTable(final String loanPrdId,final Map loanSetInfoMap,final List<Map> loanSetDetailList,final String bankId){
		//获取产品编号，查询数据库是否存在，若存在，则说明做的是更新操作，不存在，则是新增操作
		Map qryMap = new HashMap();
		qryMap.put("LoanPrdCode", loanPrdId);
		qryMap.put("BankId", bankId);
		int prdCount =loanShelfMapper.selectLoanPrdInfoCount(qryMap);
		//((Integer)this.ibsSqlMap.queryForObject("batch.selectLoanPrdInfoCount",qryMap)).intValue();
		if(prdCount > 0 ){
			//存在数据，先删除，再新增
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 数据库存在编号为："+loanPrdId+" 的该贷款套餐产品数据，先删除，再新增 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			trsTemplate.execute(new TransactionCallback(){
				public Object doInTransaction(TransactionStatus arg0) {
					deleteLoanShelfPrdById(loanPrdId, bankId);
					addLoanSetShelf(loanPrdId,loanSetInfoMap,loanSetDetailList,bankId);
					return null;
				}
			});
		}else{
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 数据库不存在编号为："+loanPrdId+" 的该贷款套餐产品数据，直接新增 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			trsTemplate.execute(new TransactionCallback(){
				public Object doInTransaction(TransactionStatus arg0) {
					addLoanSetShelf(loanPrdId,loanSetInfoMap,loanSetDetailList,bankId);
					return null;
				}
			});
		}
	}
	
	/**
	 * 套餐产品入数据库
	 * @param loanPrdId 套餐产品编号
	 * @param loanSetInfoMap 套餐产品基础信息
	 * @param loanSetDetailList 套餐产品详细信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  void addLoanSetShelf(String loanPrdId, Map loanSetInfoMap, List<Map> loanSetDetailList, String bankId){
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品基础信息插入 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		loanSetInfoMap.put("BankId", bankId);
		loanShelfMapper.insertLoanShelf(loanSetInfoMap);
//		this.ibsSqlMap.insert("batch.insertLoanShelf", loanSetInfoMap);
		log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 贷款产品详细信息插入 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		for(int i = 0; i < loanSetDetailList.size(); i++){
			log.info(">>>>>>>>>>>>>>>>>>>>>>>>> 当前为第"+(i+1)+"个贷款产品详情插入 >>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			Map detailMap = loanSetDetailList.get(i);
			detailMap.put("BankId", bankId);
			loanShelfMapper.insertLoanShelfDetail(detailMap);//产品详情
	//		this.ibsSqlMap.insert("batch.insertLoanShelfDetail", detailMap);
		}
	}
	
	/**
	 * 删除产品信息
	 * @param loanPrdId 产品编号
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected  void deleteLoanShelfPrdById(String loanPrdId, String bankId){
		Map qryMap = new HashMap();
		qryMap.put("LoanPrdCode", loanPrdId);
		qryMap.put("BankId", bankId);
		//删除详细信息表
		int a=loanShelfMapper.deleteLoanShelfDetail(qryMap);
		//删除组件信息表
		int b=loanShelfMapper.deleteLoanShelfCom(qryMap);
		//删除基础信息表
		int c=loanShelfMapper.deleteLoanShelf(qryMap);
//		this.ibsSqlMap.delete("batch.deleteLoanShelfDetail", qryMap);
	//	this.ibsSqlMap.delete("batch.deleteLoanShelfCom", qryMap);
	//	this.ibsSqlMap.delete("batch.deleteLoanShelf", qryMap);
	}
 		
	/**
	 * 修改产品状态
	 * @param loanPrdId 产品编号
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public  void   updateLoanShelfPrdState(String loanPrdId, String loanPrdStatus, String fileName, String bankId){
		//修改基础信息表产品状态
		Map upMap = new HashMap();
		upMap.put("LoanPrdCode", loanPrdId);
		upMap.put("LoanPrdStatus", loanPrdStatus);
		upMap.put("BankId", bankId);
		upMap.put("XMLFileName", ","+fileName);
	//	this.ibsSqlMap.update("batch.updateLoanShelfPrdState", upMap);
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public  void updateStatus(String fileName, Map loanHistMap, String status) {
		String times = (String) loanHistMap.get("UpdateTimes");
		int updateTimes;
		if(times != null && !"".equals(times)){
			updateTimes = Integer.parseInt(times) + 1;
		}else{
			updateTimes = 1;
		}
		Map updateMap = new HashMap();
		updateMap.put("SynFileName", fileName);
		updateMap.put("UpdateTimes", updateTimes);
		updateMap.put("Status", status);
	//	this.ibsSqlMap.update("batch.updateSynListStatus", updateMap);
	}
	
	
	
	//////////////////////
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public   List<Map> getPrdComList(Map comMap, String loanPrdId){
		List<Map> loanDetailList = new ArrayList();
		String prdComId = (String)comMap.get("@productComId");//组件编号
		String prdComName = (String)comMap.get("@name");//组件名
		Map loanComMap = new HashMap();
		Object prdFieldListObj = (Object)comMap.get("productField");//组件内容列表
		if(Util.isNullOrEmpty(prdFieldListObj)){
			return null;
		}
		if(prdFieldListObj instanceof List){
			List<Map> prdFieldList = (List)prdFieldListObj;
			for(Map prdComMap : prdFieldList){
				loanComMap = getPrdRangeMap(prdComMap, loanPrdId, prdComId, prdComName);
				if(loanComMap != null && !loanComMap.isEmpty()){
					loanDetailList.add(loanComMap);
				}
			}
		}else{
			Map prdComMap = (Map)prdFieldListObj;
			loanComMap = getPrdRangeMap(prdComMap, loanPrdId, prdComId, prdComName);
			if(loanComMap != null && !loanComMap.isEmpty()){
				loanDetailList.add(loanComMap);
			}
		}
		return loanDetailList;
	}
	
	/**
	 * 获取单个组件信息，根据组件属性id区分
	 * @param prdComMap 单个productField标签
	 * @param loanPrdId 产品编号
	 * @param prdComId 组件编号
	 * @param prdComName 组件名称
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  Map getPrdRangeMap(Map prdComMap, String loanPrdId, String prdComId, String prdComName){
		Map loanComMap = new HashMap();
		String prdFieldId = (String)prdComMap.get("@id");//属性编号
		if(prdFieldId == null || "".equals(prdFieldId) ){//|| !loanPrdComList.contains(prdFieldId)
			//属性编号为空，或者不是需要的属性编号，直接掠过
			return null;
		}
		String prdFieldName = (String)prdComMap.get("@name");//属性名称
		String prdFieldType = (String)prdComMap.get("@type");//属性值字段类型(属性值是int/String 等)
		String prdFieldLevel = (String)prdComMap.get("@level");//属性层级 产品层:1,:合约层2
		loanComMap.put("LoanPrdId", loanPrdId);
		loanComMap.put("LoanPrdComId", prdComId);
		loanComMap.put("LoanPrdComName", prdComName);
		loanComMap.put("LoanPrdFieldId", prdFieldId);
		loanComMap.put("LoanPrdFieldName", prdFieldName);
		loanComMap.put("LoanPrdFieldType", prdFieldType);
		loanComMap.put("LoanPrdFieldLevel", prdFieldLevel);
		//获取属性类型，单位，值，描述
		Map rangesMap = (Map)prdComMap.get("ranges");
		String rangeType = (String)rangesMap.get("@type");//属性类型1-枚举类型,2-范围类型
		loanComMap.put("LoanPrdRangeType", rangeType);
		StringBuffer rangeValue = new StringBuffer("");//属性值(多个，用','隔开)
		StringBuffer rangeDesc = new StringBuffer("");//属性描述(多个，用','隔开)
		Object unitListObj = (Object)rangesMap.get("unit");
		if(unitListObj instanceof List){
			List<Map> unitList = (List)unitListObj;
			String rangeVal1 = "";
			String rangeVal2 = "";
			String rangeDes1 = "";
			String rangeDes2 = "";
			for(int i = 0; i < unitList.size(); i++ ){
				Map unitMap = unitList.get(i);
				//属性类型为范围时，有属性单位，属性值类型
				if("2".equals(rangeType)){
					String unitVal = (String)(unitList.get(0).get("@val"));//属性单位
					String unitType = (String)unitMap.get("@type");//属性值类型1:上限,0:下限
					if("0".equals(unitType)){
						rangeVal1 = (String)unitMap.get("value");
						rangeDes1 = (String)unitMap.get("desc");
					}else{
						rangeVal2 = (String)unitMap.get("value");
						rangeDes2 = (String)unitMap.get("desc");
					}
					loanComMap.put("LoanPrdRangeUnit", unitVal);
				}else{
					String rangeVal = (String)unitMap.get("value");
					String rangeDes = (String)unitMap.get("desc");
					if(i < unitList.size()-1){
						rangeValue = rangeValue.append(rangeVal+",");
						rangeDesc = rangeDesc.append(rangeDes+",");
					}else{
						rangeValue = rangeValue.append(rangeVal);
						rangeDesc = rangeDesc.append(rangeDes);
					}
				}
			}
			if("2".equals(rangeType)){
				rangeValue = rangeValue.append(rangeVal1+",").append(rangeVal2);
				rangeDesc = rangeDesc.append(rangeDes1+",").append(rangeDes2);
			}
			loanComMap.put("LoanPrdRangeValue", rangeValue.toString());
			loanComMap.put("LoanPrdRangeDesc", rangeDesc.toString());
		}else{
			Map unitMap = (Map)unitListObj;
			String rangeVal = (String)unitMap.get("value");
			String rangeDes = (String)unitMap.get("desc");
			loanComMap.put("LoanPrdRangeValue", rangeVal);
			loanComMap.put("LoanPrdRangeDesc", rangeDes);
		}
		return loanComMap;
	}

	/**
	 * 获取详细信息的标题，内容
	 * @param detailMap 单个detail标签内容
	 * @param loanPrdId 产品编号
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public  Map getDetailInfoMap(Map detailMap,String loanPrdId){
		Map mm = new HashMap();
		String titleIndex = (String)detailMap.get("@index");//index:排放顺序，由0开始 
		String titleType = (String)detailMap.get("@type");//type:详情的内容类型，0:文字，1:图片，2:文件
		String detailTitle = (String)detailMap.get("title");//标题
		String detailContext = (String)detailMap.get("context");//内容
		mm.put("LoanPrdId", loanPrdId);
		mm.put("TitleIndex", titleIndex);
		mm.put("TitleType", titleType);
		mm.put("Title", detailTitle);
		mm.put("Context", detailContext);
		return mm;
	}

	public String CHARSETNAME="GBK";

	public  Map<String, Object> getLoanDat(String filePath) {
		String res=(char)0x1E+"";    
    	 File file=new File(filePath);//创建文件对象
    	 String fileName = file.getName(); // testFile.txt
		String houZhui = fileName.lastIndexOf(".") == -1 ? "" : fileName
				.substring(fileName.lastIndexOf(".") + 1) ;		
		if ("dat".equals(houZhui)) {
			BufferedReader bf = null;
			String temp = null;
			try {
				//注意:inputStreamReader是一个字节流向字符流转换的桥梁
//				bf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
				FileInputStream fis = new FileInputStream(new File(file, ""));
				
				/** 注意: 我这里吧GBK编码格式写死了, 具体工作中, 需要看txt文件的编码格式, 如果是windows系统默认, 那就是
				 * GBK, 如果是其他的, 那可以改成utf-8,
				 *  */
				bf = new BufferedReader(new InputStreamReader(fis, "GBK"));//从这一句可以看出, 这里可以指定
																			//InputStreamReader的编码格式.
				//Map<String,String> map = new  HashMap<String,String>();
				int deleteCount=loanShelfMapper.deleteLoanDat();
				log.info("删除数据条数===================="+deleteCount+"条");
				int count=0;
				while ((temp = bf.readLine()) != null) {
					count++;
						String[] arr = temp.split(res);
						Map<String,String> map = new  HashMap<String,String>();
					
						for (int i = 0; i < arr.length; i++) {
							switch(i){
							case 0:
								map.put("branch_id", arr[i]);
								break;
							case 1:
								map.put("dept_id", arr[i]);
								break;
							case 2:
								map.put("dept_name", arr[i]);
								break;
							case 3:
								map.put("area_province", arr[i]);
								break;
							case 4:
								map.put("area_city", arr[i]);
								break;
							case 5:
								map.put("area_name", arr[i]);
								break;
							case 6:
								map.put("area_road", arr[i]);
								break;
							case 7:
								map.put("branch_name", arr[i]);
								break;
							case 8:
								map.put("branch_address", arr[i]);
								break;
							case 9:
								map.put("branch_tel", arr[i]);
								break;
							case 10:
								map.put("branchx", arr[i]);
								break;
							case 11:
								map.put("branchy", arr[i]);
								break;
							case 12:
								map.put("bank_type", arr[i]);
								break;
							case 13:
								map.put("yhbm", arr[i]);
								break;
							case 14:
								map.put("iorder", arr[i]);
								break;									
							}
						}
						//将数据插入到数据库
						try {
							loanShelfMapper.insertLoanDat(map);
						} catch (Exception e) {
							log.info("数据出错===================="+(count+1)+"行"+e.getMessage());
							break;
						}
				}
				log.info("更新数据条数===================="+(count)+"条");
			} catch (Exception e) {
				log.info("txt文件读取失败");
			}finally{
				//在返回之前关闭流
				if (bf != null) {
					try {
						bf.close();
					} catch (IOException e2) {
						//添加日志文件吧
					}
				}
			}
		}else{
			System.out.println("发生异常: 文件的后缀名不是以.txt结尾的");
			 return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), "同步解析不成功");
		}
		
		 return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), "同步解析成功");
	}




}
